<!-- DeactivateAccount.vue -->
<template>
  <div class="deactivate-account">
    <div class="card">
      <div class="card-header">
        <h2>账号注销</h2>
      </div>

      <div class="warning-section">
        <div class="warning-icon">
          <i class="icon-warning"></i>
        </div>
        <div class="warning-content">
          <h3>警告</h3>
          <p>账号注销后将无法恢复，所有数据将被永久删除，请谨慎操作！</p>
        </div>
      </div>

      <div class="confirm-section">
        <label class="confirm-checkbox">
          <input type="checkbox" v-model="confirmed">
          <span>我已了解注销账号的后果</span>
        </label>
        <button
            class="btn-deactivate"
            :disabled="!confirmed"
            @click="handleDeactivate"
            :class="{ loading: loading }"
        >
          <span v-if="!loading">确认注销</span>
          <span v-else>处理中...</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DeactivateAccount',
  data() {
    return {
      confirmed: false,
      loading: false
    }
  },
  methods: {
    async handleDeactivate() {
      if (!confirm('确认注销账号？此操作不可逆！')) return;

      const uid = localStorage.getItem('uid');

      try {
        await this.$http.put('/percenter/deactivate', null, {
          params: { uid }
        });
        this.$message.success('账号已注销');
        localStorage.removeItem('uid');
        this.$router.push('/login');
      } catch (error) {
        this.$message.error('注销失败');
      }
    }
  }
}
</script>

<style scoped>
.deactivate-account {
  padding: 20px;
}

.warning-section {
  display: flex;
  align-items: center;
  padding: 20px;
  background-color: #fff8e6;
  border-radius: 4px;
  margin: 20px 0;
}

.warning-icon {
  margin-right: 15px;
}

.warning-icon i {
  font-size: 24px;
  color: #ff9800;
}

.warning-content h3 {
  margin: 0 0 5px 0;
  color: #ff9800;
}

.warning-content p {
  margin: 0;
  color: #607d8b;
}

.confirm-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}

.confirm-checkbox {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  cursor: pointer;
}

.confirm-checkbox input {
  margin-right: 8px;
}

.btn-deactivate {
  padding: 10px 20px;
  background-color: #f44336;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.btn-deactivate:disabled {
  background-color: #e0e0e0;
  color: #9e9e9e;
  cursor: not-allowed;
}

.btn-deactivate.loading {
  background-color: #f44336;
  opacity: 0.7;
}
</style>